package com.ctu.gzero.edit
{
	/**
	 * Represents range of text.
	 */
	 
	import com.ctu.gzero.elements.DOMElement;
	
	public class TextRange
	{
		private var _dom:DOMElement;
		private var _absoluteStart:int;
		private var _absoluteEnd:int;
		private var _anchorPosition:int;
		private var _activePosition:int;
		private var _endFlag:Boolean;
		
		public function TextRange(dom:DOMElement, anchorPosition:int, activePosition:int, endFlag:Boolean = false)
		{
			_dom = dom;
			_anchorPosition = anchorPosition;
			_activePosition = activePosition;
			_endFlag = endFlag;
			setStartEnd(anchorPosition, activePosition);
		}
		
		/**
		 * Update the range with new anchor or active position values.
		 */
		
		public function updateRange(newAnchorPosition:int, newActivePosition:int, endFlag:Boolean = false):Boolean
		{
			var isChanged:Boolean = false;
			if(_anchorPosition != newAnchorPosition || _activePosition != newActivePosition || _endFlag != endFlag)
			{
				isChanged = true;
				_anchorPosition = newAnchorPosition;
				_activePosition = newActivePosition;
				_endFlag = endFlag;
				setStartEnd(newAnchorPosition, newActivePosition);	
			}
			
			return isChanged;
		}
		
		public function get dom():DOMElement
		{
			return _dom;
		}
		
		public function set dom(val:DOMElement):void
		{
			_dom = val;
		}
		
		public function get absoluteStart():int
		{
			return _absoluteStart;
		}
		
		public function get absoluteEnd():int
		{
			return _absoluteEnd;
		}
		
		public function get anchorPosition():int
		{
			return _anchorPosition;
		}
		
		public function get activePosition():int
		{
			return _activePosition;
		}
		
		public function get endFlag():Boolean
		{
			return _endFlag;
		}
		
		private function setStartEnd(anchorPosition:int, activePosition:int):void
		{
			_absoluteStart = (anchorPosition <= activePosition)? anchorPosition : activePosition;
			_absoluteEnd = (anchorPosition <= activePosition)? activePosition : anchorPosition;
		}
	}
}